import { _decorator, Component, director, Label, Node } from 'cc';
const { ccclass, property } = _decorator;
import * as cc from 'cc';

@ccclass('paihangbang')
export class paihangbang extends Component {
  @property(Node)
  Leader: Node;
  onLoad() {
  }
  start() {
    globalThis.pahangbang = this;

  }

  paihangbang() {
    this.node.active = true;
  }
  paihangbangcl() {
    this.node.active = false;
  }


  //展示全部name和score
  viewPHB() {
    var bangdan = this.Leader

    //微信云开发 查询数据库操作
    let db = wx.cloud.database() //设置数据库
    let userCollection = db.collection('user_info') //单引号里为刚刚新建的集合名
    
    userCollection.field({
      user_name: true,
      user_score: true,
    }).get().then(res => {

      console.log('数据查询成功', res)

      var data = res.data;
      console.log('data = ', data);
      // 冒泡排序
      for (var i = 0; i < data.length - 1; i++) {
        for (var j = 0; j < data.length - 1 - i; j++) {
          if (data[j].user_score < data[j + 1].user_score) {
            var temp = data[j];
            data[j] = data[j + 1];
            data[j + 1] = temp;
          }
        }
      }
      console.log('newArr = ', data);

      for (let i = 0; i < data.length; i++) {
        console.log('榜单获取成功！');
        bangdan.children[i].children[1].getComponent(Label).string = data[i].user_name;
        bangdan.children[i].children[2].getComponent(Label).string = data[i].user_score;
        if (data[i].user_name === globalThis._userInfo.user_name) {
          bangdan.children[i].children[1].getComponent(Label).color = cc.Color.fromHEX(cc.color(255, 0, 0), '255');
        }
      }

    }).catch(err => {
      console.log('查询失败', err);
    });


  }

}

